package com.srd.phone.utils;

import android.content.Context;
import android.content.SharedPreferences;


public class PreferenceUtils {
    private static final String PREFERENCE_NAME = Constants.PREFERENCE_FILE_NAME;
    private static SharedPreferences mSharedPreferences;
    private static PreferenceUtils preferenceUtils;
    private static SharedPreferences.Editor editor;

    private PreferenceUtils(Context context) {
        mSharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }

    public static synchronized void init(Context cxt) {
        if (preferenceUtils == null) {
            preferenceUtils = new PreferenceUtils(cxt);
        }
    }

    public synchronized static PreferenceUtils getInstance() {
        if (preferenceUtils == null) {
            throw new RuntimeException("please init first!");
        }

        return preferenceUtils;
    }

    final public void putData(String key, String value) {
        editor.putString(key, value).commit();
    }

    final public void putData(String key, int value) {
        editor.putInt(key, value).commit();
    }

    final public void putData(String key, boolean value) {
        editor.putBoolean(key, value).commit();
    }

    final public void putData(String key, float value) {
        editor.putFloat(key, value).commit();
    }

    final public void putData(String key, long value) {
        editor.putLong(key, value).commit();
    }

    final public String getData(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    final public int getData(String key, int defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    final public boolean getData(String key, boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    final public float getData(String key, float defaultValue) {
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    final public long getData(String key, long defaultValue) {
        return mSharedPreferences.getLong(key, defaultValue);
    }

    final public void cleatData() {
        editor.clear().commit();
    }

}
